<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Story extends SMB_Controller {
	
	public function __construct() {
		parent::__construct();
		$this->load->library('data');
	}
	
	public function index() {
		$this->_get_userinfo();
		
		$this->form_validation->set_error_delimiters('<span class="error">', '</span>');
		$status = $this->input->post('update');
				
		if ($this->form_validation->run('update_story') == FALSE) {
			$studentstory = $this->data->get_story($this->userinfo['username']);
			if ($studentstory == NULL) {
				$story = '';
				$date = '';
			} else {
				$story = $studentstory->story;
				$date = $studentstory->date;
			}
			$this->_load_common_header();
			$this->_load_common_navigation();
			$this->_load_common_sidebar(get_user_sidebar($this->userinfo['role'], 'profile'));
			$this->load->view('common/tinymce');
			$params = array(
				'date' => $date,
				'story' => $story,
				'laststory' => $this->session->flashdata('laststory'),
				'updatestatus' => $this->session->flashdata('updatestatus')
			);
			$this->load->view('mahasiswa/story_update', $params);
			$this->_load_common_footer();
		} else if ($status == 'Simpan') {
			$story = $this->input->post('story', TRUE);
			$result = $this->data->update_story($this->userinfo['username'], $story);
			if ($result == FALSE) {
				$this->session->set_flashdata('laststory', $story);
				$this->session->set_flashdata('updatestatus', 'failed');
			} else {
				$this->session->set_flashdata('updatestatus', 'success');
			}
			redirect('mahasiswa/profile/story', 'refresh');
		} else {
			redirect('mahasiswa/profile/story', 'refresh');
		}
	}
}